package com.example.mybatis_plus_use.controller;

import com.example.mybatis_plus_use.entity.Orders;
import com.example.mybatis_plus_use.service.OrdersService;
import com.example.mybatis_plus_use.service.OrdersService;
import com.example.mybatis_plus_use.utils.Result; // 假设Result类在util包中
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author zjh
 * @since 2025-01-16
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/orders")
@Tag(name = "订单表", description = "订单表相关接口")
public class OrdersController extends BaseController {
    @Autowired
    @io.swagger.v3.oas.annotations.media.Schema(description = "订单服务")
    private  OrdersService ordersService;

    @Operation(summary = "分页查询订单")
    @GetMapping("/page")
    public Result<IPage<Orders>> getOrdersPage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页记录数") @RequestParam(defaultValue = "10") int size) {
        Page<Orders> page = new Page<>(current, size);
        IPage<Orders> categoryPage = ordersService.page(page, new QueryWrapper<>());
        return Result.success(categoryPage); // 返回成功结果
    }

    @Operation(summary = "查询所有订单")
    @GetMapping("/list")
    public Result<List<Orders>> getAllCategories() {
        List<Orders> categories = ordersService.list();
        return Result.success(categories); // 返回成功结果
    }

    @Operation(summary = "根据ID查询订单")
    @GetMapping("/{id}")
    public Result<Orders> getOrdersById(@PathVariable Long id) {
        Orders category = ordersService.getById(id);
        return Result.success(category); // 返回成功结果
    }

    @Operation(summary = "新增订单")
    @PostMapping
    public Result<Boolean> addOrders(@Validated @RequestBody Orders category) {
        boolean isSuccess = ordersService.save(category);
        return Result.success(isSuccess); // 返回成功结果
    }

    @Operation(summary = "更新订单")
    @PutMapping("/{id}")
    public Result<Boolean> updateOrders(@PathVariable Long id, @Validated @RequestBody Orders category) {
        category.setId(id);
        boolean isSuccess = ordersService.updateById(category);
        return Result.success(isSuccess); // 返回成功结果
    }

    @Operation(summary = "删除订单")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteOrders(@PathVariable Long id) {
        boolean isSuccess = ordersService.removeById(id);
        return Result.success(isSuccess); // 返回成功结果
    }
}
